import asyncio
from playwright.async_api import async_playwright
import time

async def xiaohongshu_login():
    """
    使用Playwright自动化小红书登录流程
    """
    async with async_playwright() as p:
        # 启动浏览器
        browser = await p.chromium.launch(headless=False)  # headless=False 可以看到浏览器操作
        context = await browser.new_context()
        page = await context.new_page()
        
        # 打开小红书官网
        print("正在打开小红书官网...")
        await page.goto("https://www.xiaohongshu.com/")
        
        # 等待页面加载
        await page.wait_for_load_state('networkidle')
        
        # 点击左边红色登录按钮
        print("正在点击登录按钮...")
        # 可能的登录按钮选择器
        login_selectors = [
            "text=登录",
            "[data-testid='login-button']",
            ".login-btn",
            "button:has-text('登录')",
            "a:has-text('登录')"
        ]
        
        login_clicked = False
        for selector in login_selectors:
            try:
                await page.click(selector, timeout=1000)
                login_clicked = True
                print(f"成功点击登录按钮: {selector}")
                break
            except:
                continue
        
        if not login_clicked:
            print("未找到登录按钮，尝试手动查找...")
            # 等待用户手动点击或查找其他方式
            await page.wait_for_timeout(1000)
        
        # 等待登录弹窗出现
        await page.wait_for_timeout(1000)
        
        # 输入手机号
        print("正在输入手机号...")
        phone_selectors = [
            "input[placeholder*='手机号']",
            "input[type='tel']",
            "input[name='phone']",
            "input[placeholder*='请输入手机号']",
            ".phone-input input"
        ]
        
        phone_input_found = False
        for selector in phone_selectors:
            try:
                await page.fill(selector, "17786136813", timeout=1000)
                phone_input_found = True
                print(f"成功输入手机号: {selector}")
                break
            except:
                continue
        
        if not phone_input_found:
            print("未找到手机号输入框，请检查页面结构")
        
        # 等待一下
        await page.wait_for_timeout(1000)
        
        # 点击同意协议
        print("正在点击同意协议...")
        agreement_selectors = [
            "input[type='checkbox']",
            ".checkbox",
            "text=同意",
            "[data-testid='agreement-checkbox']",
            "label:has-text('同意')"
        ]
        
        for selector in agreement_selectors:
            try:
                await page.click(selector, timeout=1000)
                print(f"成功点击同意协议: {selector}")
                break
            except:
                continue
        
        # 等待一下
        await page.wait_for_timeout(1000)
        
        # 点击获取验证码
        print("正在点击获取验证码...")
        verify_code_selectors = [
            "text=获取验证码",
            "button:has-text('获取验证码')",
            ".get-code-btn",
            "[data-testid='get-code-button']"
        ]
        
        for selector in verify_code_selectors:
            try:
                await page.click(selector, timeout=1000)
                print(f"成功点击获取验证码: {selector}")
                break
            except:
                continue
        
        # 等待验证码输入框出现
        await page.wait_for_timeout(1000)
        
        # 输入验证码
        print("正在输入验证码...")
        code_selectors = [
            "input[placeholder*='验证码']",
            "input[name='code']",
            "input[type='text']:not([placeholder*='手机号'])",
            ".code-input input"
        ]
        
        for selector in code_selectors:
            try:
                await page.fill(selector, "123456", timeout=1000)
                print(f"成功输入验证码: {selector}")
                break
            except:
                continue
        
        # 等待一下
        await page.wait_for_timeout(1000)
        
        # 点击登录按钮
        print("正在点击登录按钮...")
        submit_selectors = [
            "button:has-text('登录')",
            "text=登录",
            ".login-submit-btn",
            "[data-testid='login-submit']",
            "button[type='submit']"
        ]
        
        for selector in submit_selectors:
            try:
                await page.click(selector, timeout=1000)
                print(f"成功点击登录按钮: {selector}")
                break
            except:
                continue
        
        print("登录流程完成！")
        
        # 等待一段时间以观察结果
        await page.wait_for_timeout(1000)
        
        # 关闭浏览器
        await browser.close()

if __name__ == "__main__":
    # 运行登录脚本
    asyncio.run(xiaohongshu_login()) 